package com.qf.controller;

import com.qf.entity.Goods;
import com.qf.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("*")
@CrossOrigin("*")
public class GoodsController {

    @Autowired
    private IGoodsService iGoodsService;

    Map<String,String> map=new HashMap<String,String>();

    @RequestMapping("/query")
    public List<Goods> selectAll(){
        List<Goods> goods = iGoodsService.selectAll();
        return goods;
    }

    @RequestMapping("/selectGoods/{title}")
    public Goods selectGood(@PathVariable("title") String title){
        Goods goods = iGoodsService.selectGoods(title);
        return goods;
    }

    @RequestMapping("/selectById/{goodIdsStr}")
    public List<Goods> selectById(@PathVariable("goodIdsStr") String goodsId){
        String[] idsArr = goodsId.split(",");
        Integer[] gId = new Integer[idsArr.length];
        for (int i=0;i<gId.length;i++){
            gId[i] = Integer.parseInt(idsArr[i]);
        }
        List<Goods> goods = iGoodsService.selectById(gId);
        return goods;
    }

    @RequestMapping("/selectNum/{goodIdsStr}/{stockNum}")
    public List<Goods> selectById(@PathVariable("goodIdsStr") String goodsId,@PathVariable("stockNum") String stockNum){
        String[] idsArr = goodsId.split(",");
        Integer[] gId = new Integer[idsArr.length];
        for (int i=0;i<gId.length;i++){
            gId[i] = Integer.parseInt(idsArr[i]);
        }
        String[] stock = stockNum.split(",");
        Integer[] num = new Integer[idsArr.length];
        for (int i=0;i<num.length;i++){
            num[i] = Integer.parseInt(stock[i]);
        }
        List<Goods> goods = iGoodsService.selectInventory(gId, num);
        return goods;
    }
}
